<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/26 0026
 * Time: 16:42
 */

namespace app\wap\controller;


use Firebase\JWT\JWT;
use think\Cache;
use think\Db;

class Cl extends Base
{

    private $uid = 0;
    private $vip;
    public function __construct()
    {
        parent::__construct();
        $Key = config('LOGINKEY');
        $token = input('token');
        if(!$token)exit($this->encode('请登录'));
        $ref = JWT::decode($token,$Key,['HS256']);
        if(!$token || !is_object($ref))exit($this->encode('登录信息错误,请重新登录'));
        $this->uid = $ref->uid;
        $this->vip = Db::name('user_service')->where(['uid'=>$this->uid])->value('vip');
    }


    //todo 策略跟单币种
    public function ClCurrency(){
      	
        $list = Db::name('transaction')->field('title,num as price')->select();
      	foreach($list as $k=>$v){
        	$list[$k]['title'] =  strtoupper($v['title']);
          	if($this->vip != 2)$list[$k]['price'] = '*';
        }
      	
        return $this->returnSuccess($list);
    }
  
  	
  
    public function ClPrice(){
        $money = $this->redis->get('money');
        $int = 10000;
      	$res = Db::name('transaction')->field('title,num')->select();
      	$prices = Cache::get('huobi');
      	foreach($res as $k=>$v){
          	foreach($prices as $ke=>$va){
            	if($va['name'] == $v['title']){
               		$money += $va['price']*$v['num']; 	
                }
            }
        }
        return $this->returnSuccess(['price'=>$int,'nowprice'=>round($money,2),'lv'=>round(($money-$int)/$int,2)]);
    }

    public function Cl(){
        $map = ['uid'=>$this->uid];
        $ref = Db::name('user_service')->where($map)->value('cl');
        return $this->returnSuccess($ref);
    }

    public function ClOrder(){
        $time = Db::name('trading_order')->order('create_time desc')->value('create_time');
      	$map = ['create_time'=>['<',$time+3600]];
        if($this->vip != 2)$map['create_time'] = ['<',$time-3600*48] ;
        $page = $this->page($this->data);
        $list = Db::name('trading_order')
            ->where($map)
            ->limit($page['page'],$page['limit'])
            ->order('create_time desc')
            ->select();
      	foreach($list as $k=>$v){
          	$list[$k]['total'] = $v['price'];
        	$list[$k]['title'] = strtoupper($v['title']);
        }
        return $this->returnSuccess($list);
    }
}